﻿using NfsBLL;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MobileWeb.Controllers
{
    [BaseController(IsCheck = false)]
    public class ShenQingController : Controller
    {
        //
        // GET: /ShenQing/
        public ActionResult Index()
        {
            return View();
        }
        #region 绑定下拉框类别
        public JsonResult GetCateAndLev()
        {
            try
            {
                CategoryBLL Catebll = new CategoryBLL();
                List<category> Catelist = Catebll.GetModelList(" valid=1");
                LevelBLL Levbll = new LevelBLL();
                List<level> Levlist = Levbll.GetModelList(" valid=1");
                return Json(new { MessageCode = "0", Catelist = Catelist, Levlist = Levlist}, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }

        }
        #endregion
        #region 修改和新增用户，以及授权
        [HttpPost]
        public JsonResult OpeaShenQing()
        {
            try
            {
                bool flag = true;
                int id = Request["id"] != null ? int.Parse(Request["id"].ToString()) : 0;
                int sh_id = Request["sh_id"] != null ? int.Parse(Request["sh_id"].ToString()) : 0;
                string Name = Request["Name"] != null ? Request["Name"] : "";
                string companyName = Request["companyName"] != null ? Request["companyName"] : "";
                string contract_name = Request["contract_name"] != null ? Request["contract_name"] : "";
                string mobile = Request["mobile"] != null ? Request["mobile"] : "";
                string qq = Request["qq_code"] != null ? Request["qq_code"] : "";
                string wxOpenid = Request["wxOpenid"] != null ? Request["wxOpenid"] : "";
                string wwCode = Request["wwCode"] != null ? Request["wwCode"] : "";
                string idCard = Request["idCard"] != null ? Request["idCard"] : "";
                string shopUrl = Request["shopUrl"] != null ? Request["shopUrl"] : "";
                int category_id = Request["category_id"] != null ? int.Parse(Request["category_id"]) : 0;
                int level_id = Request["level_id"] != null ? int.Parse(Request["level_id"]) : 0;
                string level_name = Request["level_name"] != null ? Request["level_name"] : "";
                ShenqingBLL bll = new ShenqingBLL();
                AgencyBLL agBll = new AgencyBLL();
                if (!agBll.GetSingle(id, Name, mobile, qq, wxOpenid, wwCode, idCard, shopUrl))
                {
                    shenqing model = new shenqing();
                    model.id = id;
                    model.account_id = id;
                    model.parents_id = 0;
                    model.name = Name;
                    model.company_name = companyName;
                    model.contract_name = contract_name;
                    model.mobile = mobile;
                    model.qq = qq;
                    model.wx_openid = wxOpenid;
                    model.ww_code = wwCode;
                    model.id_card = idCard;
                    model.shop_url = shopUrl;
                    model.state = 0;
                    model.valid = 1;
                    model.level_id = level_id;
                    model.category_id = category_id;
                    model.level_name = level_name;
                    model.shState = 0;//表示未审核
                    account act_model = new account { user_name = mobile, user_pwd = mobile.Substring(5, 6), type = 2, IsAdmin = 0, mobile = mobile, valid = 1, time = DateTime.Now };
                    flag = bll.Add(model,out id);
                    if (flag)
                    {
                      
                            return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                     

                    }
                    else
                    {
                        return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);


                    }
                }
                else
                {
                    return Json(new { MessageCode = "50" }, JsonRequestBehavior.AllowGet);

                }


            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }

        }
        #endregion
	}
}